package com.srwl.mytx.adapter;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.Constant;
import com.srwl.mytx.EaseConstant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.ForwardDetailsActivity;
import com.srwl.mytx.activity.TopicDetailsActivity;
import com.srwl.mytx.activity.UserProfileActivity;
import com.srwl.mytx.domain.Forward;
import com.srwl.mytx.domain.Topic;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReceivedForwardRecordAdapter extends RecyclerView.Adapter<ReceivedForwardRecordAdapter.ReceivedForwardViewHolder> {
    private static final String TAG = "ReceivedForwardRecordAdapter";
    private final List<Forward> forwardList;
    private final Context context;
    private InteractionListener listener;


    public ReceivedForwardRecordAdapter(Context context, List<Forward> forwardList) {
        this.forwardList = forwardList;
        this.context = context;

    }


    @Override
    public ReceivedForwardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ReceivedForwardViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_donation_record_to_topic_had_forward_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final ReceivedForwardViewHolder viewHolder, final int position) {
        final Forward forward = forwardList.get(position);
        final Topic targetTopic = forward.getAttachTopic();//显示主体（主题）信息

        if (targetTopic.getPictures() != null && targetTopic.getPictures().size() != 0) {
            viewHolder.iv_topicIcon.setVisibility(View.VISIBLE);
            //RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate();
            //Glide.with(context).load(targetTopic.getPictures().get(0)).apply(options).into(viewHolder.iv_topicIcon);
            GlideLoadUtils.glideLoad(context,targetTopic.getPictures().get(0),viewHolder.iv_topicIcon);
        } else {
            viewHolder.iv_topicIcon.setVisibility(View.INVISIBLE);
        }
        viewHolder.tv_topicTitle.setText(targetTopic.getTitle());
        viewHolder.tv_address.setText(targetTopic.getAddress());

        final User targetUser = forward.getTargetUser();//经手人信息
       // Glide.with(context).load(targetUser.getAvatar()).diskCacheStrategy(DiskCacheStrategy.ALL).error(R.drawable.tx_default_avatar).into(viewHolder.iv_avatar);
        GlideLoadUtils.glideLoad(context,targetUser.getAvatar(),viewHolder.iv_avatar,R.drawable.tx_default_avatar_1);
        viewHolder.tv_name.setText(targetUser.getUsername());


        viewHolder.tv_forwardReason.setText(forward.getReason());

        viewHolder.tv_amount.setText(Utils.moneyFormat(forward.getAmount()));
        DateFormat dateFormat = SimpleDateFormat.getDateInstance(2);
        viewHolder.tv_date.setText(dateFormat.format(forward.getCreated()));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, ForwardDetailsActivity.class).putExtra(Constant.EXTRA_INFO_TOPIC, forward));

            }
        });
        viewHolder.topicInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, TopicDetailsActivity.class).putExtra(Constant.EXTRA_INFO_TOPIC, targetTopic));
            }
        });

        viewHolder.userInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, UserProfileActivity.class).putExtra(EaseConstant.EXTRA_TARGET_USER_INFO, targetUser));

            }
        });

    }


    @Override
    public int getItemCount() {
        return forwardList.size();
    }


    class ReceivedForwardViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_avatar)
        ImageView iv_avatar;
        @BindView(R.id.tv_nick)
        TextView tv_name;
        @BindView(R.id.tv_amount)
        TextView tv_amount;
        @BindView(R.id.tv_date)
        TextView tv_date;
        @BindView(R.id.tv_extra_message)
        TextView tv_forwardReason;
        @BindView(R.id.iv_topic_icon)
        ImageView iv_topicIcon;
        @BindView(R.id.tv_topic_title)
        TextView tv_topicTitle;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.ll_topic_info)
        View topicInfo;
        @BindView(R.id.ll_user_info)
        View userInfo;


        public ReceivedForwardViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public interface InteractionListener {
        void onShowTopicDetail(String topicId);

        void onShowForwardDetail(String forwardId);

        void revokeDonation(String donationId);
    }

}